﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcShop.Extensions
{
    public interface IPagedList
    {
        int TotalPages { get; set; }
        int TotalCount { get; set; }
        int PageIndex { get; set; }
        int PageSize { get; set; }
        bool IsPreviousPage { get; }
        bool IsNextPage { get; }
    }
    /// <summary>
    /// 自定分页控件
    /// </summary>
    /// <typeparam name="T">传入的Model</typeparam>
    public class PagedList<T> : List<T>, IPagedList
    {
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="source">需分页的类型集合</param>
        /// <param name="index">当前页</param>
        /// <param name="pageSize">页显示的数量</param>
        public PagedList(IQueryable<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.TotalPages = (int)Math.Round((decimal)TotalCount / (decimal)pageSize);
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.IsPreviousPage = index > 1;
            this.IsNextPage = index < TotalPages;
            this.AddRange(source.Skip((index - 1) * pageSize).Take(pageSize).ToList());
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="source">需分页的类型集合</param>
        /// <param name="index">当前页</param>
        /// <param name="pageSize">页显示的数量</param>
        public PagedList(List<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.TotalPages = (int)Math.Round((decimal)TotalCount / (decimal)pageSize);
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.IsPreviousPage = index > 1;
            this.IsNextPage = index < TotalPages;
            this.AddRange(source.Skip((index - 1) * pageSize).Take(pageSize).ToList());
        }

        public int TotalPages { get; set; }
        public int TotalCount { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public bool IsPreviousPage
        {
            get;
            set;
            //get
            //{
            //    return (PageIndex > 0);
            //}
        }
        public bool IsNextPage
        {
            get;
            set;
            //get
            //{
            //    return (PageIndex * PageSize) <= TotalCount;
            //}
        }
    }

    public static class Pagination
    {
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
        {
            return new PagedList<T>(source, index, pageSize);
        }

        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        {
            return new PagedList<T>(source, index, 10);
        }

        public static PagedList<T> ToPagedList<T>(this List<T> source, int index, int pageSize)
        {
            return new PagedList<T>(source, index, pageSize);
        }

        public static PagedList<T> ToPagedList<T>(this List<T> source, int index)
        {
            return new PagedList<T>(source, index, 10);
        }
    }
}
